package com.example.shushiyunsst.fragment.main.home.child.miner22

import android.os.Bundle
import android.view.View
import com.alibaba.fastjson.JSON
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.album.utils.Utils
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.miner22.bean.ToCountListBean
import com.example.shushiyunsst.fragment.main.home.child.miner22.bean.ToCountListHeaderBean
import com.example.wallet.db.UserInfoManager
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

class ToCountListFragment : SimpleListFragment<ToCountListBean>() {

    companion object {
        fun newInstance(): ToCountListFragment {
            val args = Bundle()
            val fragment = ToCountListFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var toCountListener:ToCountListener?=null

    fun setToCountListener(toCountListener:ToCountListener):ToCountListFragment{
        this.toCountListener=toCountListener
        return this
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
        isEnableLoadMore = false
        isAddDividerItemDecoration = false
    }

    override fun initView(view: View) {
        super.initView(view)
        mRecyclerView.overScrollMode = View.OVER_SCROLL_NEVER
    }

    override fun getItemLayout(): Int {
        return R.layout.to_count_list_fragment
    }

    override fun convertItem(helper: JssBaseViewHolder?, item: ToCountListBean?) {
        helper?.setText(R.id.accountName, item?.accountName)
            ?.setText(R.id.cmlBalance, item?.cmlBalance.toString())
            ?.setText(R.id.yesterdayIncome, "昨日算力：${item?.yestodayIncome}")
            ?.setText(R.id.interestTv, "累计生息：${item?.cmlIncome}")
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(ToCountListBean::class.java).endSubType().build()
    }

    override fun netRequest() {
//        val jsonFromRaw = Utils.getJsonFromRaw(_mActivity, R.raw.temp_fack_data)
//        val jsonFromRaw =""
//            parseDate(jsonFromRaw)

        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo

        val map = mapOf(
            "uid" to userInfo?.uid
        )

        RestClient.builder()
            .raw(JSON.toJSONString(map))
            .url(R.string.miningPoolCount)
            .success {
                parseDate(it)
            }
            .failure {
                onFailed()
            }
            .error { _, _ ->
                onFailed()
            }.build().post()
    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val obj = JSON.parseObject(data)

        val totalIncome = obj["totalIncome"].toString()

        val myTotalIncome = obj["myTotalIncome"].toString()

        val myShareIncome = obj["myShareIncome"].toString()

        val mySpreadIncome = obj["mySpreadIncome"].toString()

        val bestHave = obj["bestHave"].toString()

        val minCostIncome = obj["minCostIncome"].toString()

        val headerBean = ToCountListHeaderBean(
            totalIncome,
            myTotalIncome,
            myShareIncome,
            mySpreadIncome,
            bestHave,
            minCostIncome
        )

        toCountListener?.setToCount(headerBean)

        val childAccList = obj["childAccList"]
        val array = JSON.parseArray(childAccList.toString(), ToCountListBean::class.java)
        loadListDate(array)
    }
}